home *** CD-ROM | disk | FTP | other *** search
/ Experimental BBS Explossion 3 / Experimental BBS Explossion III.iso / c / cujoct93.zip / 1110072C < prev    next >
Text File  |  1993-01-19  |  4KB  |  125 lines

  1. /********************* DATACOMP.C *********************/
  2. #include <alloc.h>
  3. #include <conio.h>
  4. #include "randefs.h"
  5. #include "datacomp.h"
  6. #include "displays.h"
  7.  
  8. int MakeHistogram(RAND *dat)
  9. { int    i, nbin = dat->nbin;
  10.   index  *hstogm;
  11.   float  *frqdst;
  12.  
  13.   NewScreen(*(dat->labls+nbin) );
  14.   DrawAxes(dat);
  15.   frqdst = (float *)calloc(nbin, sizeof(float) );
  16.   hstogm = (index *)calloc(nbin, sizeof(index) );
  17.   while ( (dat->ndat >= 0) && (kbhit() == 0) )
  18.   {  dat->ev_indx = SumNPicks(dat);
  19.      UpdateDistrib(dat, hstogm, frqdst);
  20.      DisplayFreq(dat, frqdst);
  21.   }
  22.   if (ESC == getch())  /* User's chance to exit */
  23.   {  free( (void *)frqdst );
  24.      free( (void *)hstogm );
  25.      return(1);
  26.   }
  27.   DisplayStats(dat, frqdst);
  28.   free( (void *)frqdst );
  29.   free( (void *)hstogm );
  30.   if (ESC == getch())  /* User's chance to exit */
  31.      return(1);
  32.  
  33.   return(0);
  34. }
  35.  
  36. void UpdateDistrib(RAND *dat, index *hstogm, float *frqdst)
  37. { int   i, nd = ++dat->ndat, nbin  = dat->nbin;
  38.   int   indx  = dat->ev_indx;
  39.   float k0    = 1/(nd*dat->delta);
  40.  
  41.   for (i = 0; i < nbin; i++)
  42.      if (indx == i)
  43.      {  ++(*(hstogm+i)); /* Add 1 to bin */
  44.         break;
  45.      }
  46.   for (i = 0; i < nbin; i++)
  47.      *(frqdst+i) = k0*(float)(*(hstogm+i));
  48. }
  49.  
  50. void AddNoise(int *in, int *out, RAND *noise)
  51. { int   i, nbin = noise->nbin;
  52.   int   *input = in, *noisydat = out;
  53.   float sf;
  54.  
  55.   /******* Amplitude scaling factors ********/
  56.   if (nbin < 6)         /* 1 and 2 coins    */
  57.      sf = 5;
  58.   else if (nbin < 11)   /* Single die       */
  59.      sf = 2;
  60.   else if (nbin == 24)  /* Uniform Circular */
  61.      sf = 0.5;
  62.   else                  /* Dice and normal  */
  63.      sf = 0.8;
  64.  
  65.   *noisydat++ = *input++;    /* Copy the data size */
  66.   for (i = 0; i < SPAN; i++)
  67.      *noisydat++ = *input++ + sf*(2*SumNPicks(noise)-(nbin-1));
  68. }
  69.  
  70. void MakeScopeSignals(RAND *dat, int wt_tbl[][MAXWTLEN])
  71. { int  i, nbin = dat->nbin, *signalbuf, *auxbuf, *wts;
  72.  
  73.   /******** Simulated Oscilloscope Signals ********/
  74.   NewScreen( *(dat->labls+nbin) );
  75.   ScreenLabels();
  76.   signalbuf = (int *)calloc(SPAN, sizeof(int) );
  77.   auxbuf    = (int *)calloc(SPAN, sizeof(int) );
  78.   /******** Generate Simulated Square Wave ********/
  79.   MakeSqWave(signalbuf, 5);
  80.   DrawData(signalbuf, 0.98*FS);     /* Display it */
  81.   /************* Add Noise to Signal **************/
  82.   AddNoise(signalbuf, auxbuf, dat);
  83.   DrawData(auxbuf, 0.76*FS);        /* Display it */
  84.   /************ Filter the Noisy Signal ***********/
  85.   for (i = 0; i < 3; i++)
  86.   {   Filter(auxbuf, signalbuf, wt_tbl[i] );
  87.       DrawData(signalbuf, (0.54-i*0.22)*FS );
  88.   }
  89.   free((void *)auxbuf);
  90.   free((void *)signalbuf);
  91. }
  92.  
  93. void MakeSqWave(int *data, int n_halfcycls)
  94. { int  i, k, *dat = data;
  95.  
  96.   *dat++ = SPAN;      /* 1st entry = data size */
  97.   for (k = 0; k < n_halfcycls; k++)
  98.      for (i = 0; i < SPAN/n_halfcycls; i++)
  99.         *dat++ = !(k%2) ? 0 : -50;
  100.  
  101.   while ( (dat-data-1) < SPAN )
  102.      *dat++ = 0;       /* Clean-up */
  103. }
  104.  
  105. void Filter(int *rawdat, int *filtrdat, int *wt_tbl)
  106. { int  *wt  = wt_tbl, *raw = rawdat, *new = filtrdat;
  107.   int  i, i0, k, nwts, ndat, norm;
  108.  
  109.   nwts = *wt++;  /* size of wt vector = odd no. */
  110.   norm = *wt++;  /* Normalization factor        */
  111.   ndat = *raw++; /* size of data                */
  112.   i0   = nwts/2;
  113.  
  114.   *new++ = ndat-2*i0;  /* Size of filtered data */
  115.  
  116.   for (i = i0; i < ndat-i0; i++)
  117.   {  *new = 0;
  118.      for (k = 0; k < nwts; k++)
  119.         *new += *(raw+k)*(*(wt+k));  /* Get wt'd sum */
  120.  
  121.      *new++ /= norm;                 /* Normalize it */
  122.      raw++;
  123.   }
  124. }
  125.